package com.example.mvcdemo.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/4
 * Time: 17:46
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Boolean login(HttpServletRequest request, String userName, String password) {
//        if (userName == null || "".equals(userName)) {
//            return false;
//        }

        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        // 不为空，校验账号和密码是否正确
        if ("admin".equals(userName) && "admin".equals(password)) {
            // 设置 session
            HttpSession session = request.getSession(true);
            session.setAttribute("userName", userName);
            return true;
        }
        return false;

//        if (!"admin".equals(userName) || !"admin".equals(password)) {
//            return false;
//        }
//        return true;
    }

    @RequestMapping("/getLoginUser")
    public String getLoginUser(HttpSession session) {
        if (session.getAttribute("userName") != null) {
            return (String)session.getAttribute("userName");
        }
        return "";
    }
}
